#include "vm.hpp"
#include "actor.hpp"
#include <string>
#include <iostream>
#include <sstream>
#include <boost/tokenizer.hpp>
#include "argument_separator.hpp"

using namespace BMD;
using std::string;
using std::cout;
using std::endl;
using std::cerr;
using std::cin;
using std::pair;
using std::ostream_iterator;
using std::istringstream;
using std::map;
using std::make_pair;

/**** single tests files ****/
string test_files [] = {
  "tests/fraction.bmdo", 
  "tests/function.bmdo",
  "tests/list.bmdo",
  "tests/array.bmdo",
  "tests/filledblock.bmdo",
  "tests/init.bmdo",
  "tests/apply.bmdo",
  "tests/factorial.bmdo",
  "tests/error.bmdo",
  "tests/boolean.bmdo",
  "tests/nil.bmdo",
  "tests/staticblock.bmdo",
  "tests/comparison.bmdo",
  "tests/int_arith.bmdo",
  "tests/string.bmdo"
};

/**** linked file tests ****/
pair<string, string> linked_test_files [] = { 
  make_pair("tests/rpc1_A.bmdo", "tests/rpc1_B.bmdo"),
  make_pair("tests/rpc_adderA.bmdo", "tests/rpc_adderB.bmdo")
};

void test_bmd_file(string filename)
{
  shared_ptr<Actor> actor(new Actor());
  
  // loading the file can give parsing errors 
  actor->load_from_file(filename);
  
  try
  {
    actor->run_with_assertions();
  }
  catch (assert_test_error e)
  {
    ostringstream oss;
    oss << "at ip " <<  actor->get_vm()->get_instruction_pointer()
        << ": " << e.what();
    throw assert_test_error(oss.str());
  }
  catch (runtime_error e)
  {
    if (!actor->should_throw_error())
      throw;
  }
}

void test_linked_bmd_files(string file1, string file2)
{
  shared_ptr<Actor> actor1(new Actor());
  shared_ptr<Actor> actor2(new Actor());
 
  try
  {
    // load the actors and link them together
    actor1->load_from_file(file1);
    actor2->load_from_file(file2);
    actor1->set_linked_actor(actor2);
    actor2->set_linked_actor(actor1);
    
    actor1->run_with_assertions();
  }
  catch (assert_test_error e)
  {
    ostringstream oss;
    oss << "at " <<  actor1->get_vm()->get_instruction_pointer()
        << ": " << e.what();
    throw assert_test_error(oss.str());
  }
  catch (runtime_error e)
  {
    if (!actor1->should_throw_error())
      throw;
  }
}

int run_single_tests(int& test_idx)
{
  const int num_test_files = sizeof(test_files)/sizeof(string);
  int num_tests_passed = 0;

  /**** single file tests ****/
  for (int i = 0; i < num_test_files; ++test_idx, ++i)
  {
    cout << "Test " << test_idx << ": " << test_files[i] << endl;
    try
    {
      test_bmd_file(test_files[i]);
    }
    catch (runtime_error e)
    {      
      cout << "Test " << test_idx << " failed: " << e.what() << endl;
      continue;
    }

    cout << "Test " << test_idx << " succeeded." << endl;
    ++num_tests_passed;
  }
  return num_tests_passed;
}

int run_linked_tests(int& test_idx)
{
  const int num_linked_test_files = 
    sizeof(linked_test_files)/sizeof(pair<string, string>);
  
  int num_tests_passed = 0;
  for (int i = 0; i < num_linked_test_files; ++i, ++test_idx)
  {
    
    cout << "Test " << test_idx << ": " 
         << linked_test_files[i].first << " and "
         << linked_test_files[i].second << endl;
    try
    {
      test_linked_bmd_files(linked_test_files[i].first, linked_test_files[i].second);
    }
    catch (runtime_error e)
    {
      cout << "Test " << test_idx << " failed: " << e.what() << endl;
      continue;
    }

    cout << "Test " << test_idx << " succeeded." << endl;
    ++num_tests_passed;
  }
  
  return num_tests_passed;
}

int main (int argc, char** args)
{
  int test_idx = 1;
  int num_passed = 0;
  num_passed += run_single_tests(test_idx);
  num_passed += run_linked_tests(test_idx);

  cout << "Tests passed " << num_passed << "/" << (test_idx - 1) << endl;

  if (num_passed == test_idx - 1)
    return 0;
  else
    return 1;
}

